﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
   public class TblUserAccountDAO
    {
         private DBService _db = null;

         public TblUserAccountDAO(string ConnectionString)
         {
            _db = new DBService(ConnectionString);
         }

       // Insert - Update UserAccount

         public bool Insert_Update(TblUserAccount user, int mode)
         {      
             bool flag = false;
             try
             {
                 int result = 0;
                 result = _db.ExeSP_NonQuery("sp_TblUserAccount_InsUpd",
                     new string[] {"@AccountID", "@Email", "@Password", "@AccountStatus", "@FirstName", "@LastName", "@Newsletter", "@Mode"},
                     new object[] {user.AccountID, user.Email, user.Password, user.AccountStatus, user.FirstName, user.LastName, user.Newsletter, mode},
                     CommandType.StoredProcedure);

                 flag = true;
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return flag;
         }

         // Check Email User 

         public bool CheckExits(string mail, int mode)
         {
             bool flag = false;
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("TblUserAccount_CheckExists",
                     new string[] { "@Email", "@mode" },
                     new object[] { mail, mode },
                     CommandType.StoredProcedure);
                 if (dt.Rows.Count > 0)
                 {
                     flag = true;
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return flag;
         }

       // Get User 
         public TblUserAccount GetUser(string emailaddress, string pass)
         {
             TblUserAccount user = new TblUserAccount();
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_Check",
                    new string[] { "@Email", "@Password" },
                    new object[] { emailaddress, pass },
                   CommandType.StoredProcedure);

                 foreach (DataRow dr in dt.Rows)
                 {
                     user.Email = dr["Email"].ToString();
                     user.AccountID = Convert.ToInt32(dr["AccountID"]);
                     user.AccountStatus = Convert.ToInt32(dr["AccountStatus"]);
                     user.LastLogin = Convert.ToDateTime(dr["LastLogin"]);
                     user.LastName = dr["LastName"].ToString();
                     user.FirstName = dr["FirstName"].ToString();
                     user.Password = pass;
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return user ;
         }

       // Admin Get

         public TblUserAccount GetAdmin(string Email, string PassWord)
         {
             TblUserAccount user = new TblUserAccount();
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_AdminCheck",
                    new string[] { "@Email", "@Password" },
                    new object[] { Email, PassWord },
                   CommandType.StoredProcedure);

                 foreach (DataRow dr in dt.Rows)
                 {
                     user.Email = dr["Email"].ToString();
                     user.AccountID = Convert.ToInt32(dr["AccountID"]);
                     user.AccountStatus = Convert.ToInt32(dr["AccountStatus"]);
                     user.LastLogin = Convert.ToDateTime(dr["LastLogin"]);
                     user.LastName = dr["LastName"].ToString();
                     user.FirstName = dr["FirstName"].ToString();
                     user.Password = dr["Password"].ToString();
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return user;
         }

         public TblUserAccount GetAll(string Email)
         {
             TblUserAccount user = new TblUserAccount();
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_Get",
                    new string[] { "@Email"},
                    new object[] { Email},
                   CommandType.StoredProcedure);

                 foreach (DataRow dr in dt.Rows)
                 {
                     user.Email = dr["Email"].ToString();
                     user.AccountID = Convert.ToInt32(dr["AccountID"]);
                     user.AccountStatus = Convert.ToInt32(dr["AccountStatus"]);
                     //user.LastLogin = Convert.ToDateTime(dr["LastLogin"]);
                     user.LastName = dr["LastName"].ToString();
                     user.FirstName = dr["FirstName"].ToString();
                     user.ActivatedCode = dr["ActivatedCode"].ToString();
                     
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return user;
         }

         //public DataTable ResetPass()
         //{
         //    DataTable pass = null;
         //    try
         //    {
         //        pass = _db.ExeSP_DataTable("sp_TblUserAccount_ResetPass",
         //            false,
         //            CommandType.StoredProcedure);
         //    }
         //    catch (Exception ex)
         //    {
         //        throw ex;
         //    }
         //    return pass;
         //}

         //public string ResetPass()
         //{
         //    string pass = "";
         //    try
         //    {
         //        pass = _db.ExeSP_DataTable("select (right(newid(),6))", false, CommandType.Text).ToString();
         //    }
         //    catch (Exception ex)
         //    {
         //        throw ex;
         //    }
         //    return pass;
         //}
    }
}
